home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Produtividade / Software2 / Product4 / Setup.exe / drupal-4.6.0 / includes / xmlrpcs.inc < prev   
Encoding:
Text File  |  2004-01-06  |  10.0 KB  |  303 lines

  1. <?php
  2. // by Edd Dumbill (C) 1999-2001
  3. // <edd@usefulinc.com>
  4. // $Id: xmlrpcs.inc,v 1.9 2004/01/06 12:09:41 unconed Exp $
  5.  
  6. // Copyright (c) 1999,2000,2001 Edd Dumbill.
  7. // All rights reserved.
  8. //
  9. // Redistribution and use in source and binary forms, with or without
  10. // modification, are permitted provided that the following conditions
  11. // are met:
  12. //
  13. //    * Redistributions of source code must retain the above copyright
  14. //      notice, this list of conditions and the following disclaimer.
  15. //
  16. //    * Redistributions in binary form must reproduce the above
  17. //      copyright notice, this list of conditions and the following
  18. //      disclaimer in the documentation and/or other materials provided
  19. //      with the distribution.
  20. //
  21. //    * Neither the name of the "XML-RPC for PHP" nor the names of its
  22. //      contributors may be used to endorse or promote products derived
  23. //      from this software without specific prior written permission.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. // REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  34. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  36. // OF THE POSSIBILITY OF SUCH DAMAGE.
  37.  
  38. // XML RPC Server class
  39. // requires: xmlrpc.inc
  40.  
  41. // listMethods: either a string, or nothing
  42. $_xmlrpcs_listMethods_sig=array(array($xmlrpcArray, $xmlrpcString),
  43.                                 array($xmlrpcArray));
  44. $_xmlrpcs_listMethods_doc='This method lists all the methods that the XML-RPC server knows how to dispatch';
  45. function _xmlrpcs_listMethods($server, $m) {
  46.   global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
  47.   $v=new xmlrpcval();
  48.   $dmap=$server->dmap;
  49.   $outAr=array();
  50.   for(reset($dmap); list($key, $val)=each($dmap); ) {
  51.     $outAr[]=new xmlrpcval($key, "string");
  52.   }
  53.   $dmap=$_xmlrpcs_dmap;
  54.   for(reset($dmap); list($key, $val)=each($dmap); ) {
  55.     $outAr[]=new xmlrpcval($key, "string");
  56.   }
  57.   $v->addArray($outAr);
  58.   return new xmlrpcresp($v);
  59. }
  60.  
  61. $_xmlrpcs_methodSignature_sig=array(array($xmlrpcArray, $xmlrpcString));
  62. $_xmlrpcs_methodSignature_doc='Returns an array of known signatures (an array of arrays) for the method name passed. If no signatures are known, returns a none-array (test for type != array to detect missing signature)';
  63. function _xmlrpcs_methodSignature($server, $m) {
  64.   global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
  65.  
  66.   $methName=$m->getParam(0);
  67.   $methName=$methName->scalarval();
  68.   if (ereg("^system\.", $methName)) {
  69.     $dmap=$_xmlrpcs_dmap; $sysCall=1;
  70.   } else {
  71.     $dmap=$server->dmap; $sysCall=0;
  72.   }
  73.   //  print "<!-- ${methName} -->\n";
  74.   if (isset($dmap[$methName])) {
  75.     if ($dmap[$methName]["signature"]) {
  76.       $sigs=array();
  77.       $thesigs=$dmap[$methName]["signature"];
  78.       for($i=0; $i<sizeof($thesigs); $i++) {
  79.         $cursig=array();
  80.         $inSig=$thesigs[$i];
  81.         for($j=0; $j<sizeof($inSig); $j++) {
  82.           $cursig[]=new xmlrpcval($inSig[$j], "string");
  83.         }
  84.         $sigs[]=new xmlrpcval($cursig, "array");
  85.       }
  86.       $r=new xmlrpcresp(new xmlrpcval($sigs, "array"));
  87.     } else {
  88.       $r=new xmlrpcresp(new xmlrpcval("undef", "string"));
  89.     }
  90.   } else {
  91.       $r=new xmlrpcresp(0,
  92.               $xmlrpcerr["introspect_unknown"],
  93.               $xmlrpcstr["introspect_unknown"]);
  94.   }
  95.   return $r;
  96. }
  97.  
  98. $_xmlrpcs_methodHelp_sig=array(array($xmlrpcString, $xmlrpcString));
  99. $_xmlrpcs_methodHelp_doc='Returns help text if defined for the method passed, otherwise returns an empty string';
  100. function _xmlrpcs_methodHelp($server, $m) {
  101.   global $xmlrpcerr, $xmlrpcstr, $_xmlrpcs_dmap;
  102.  
  103.   $methName=$m->getParam(0);
  104.   $methName=$methName->scalarval();
  105.   if (ereg("^system\.", $methName)) {
  106.     $dmap=$_xmlrpcs_dmap; $sysCall=1;
  107.   } else {
  108.     $dmap=$server->dmap; $sysCall=0;
  109.   }
  110.   //  print "<!-- ${methName} -->\n";
  111.   if (isset($dmap[$methName])) {
  112.     if ($dmap[$methName]["docstring"]) {
  113.       $r=new xmlrpcresp(new xmlrpcval($dmap[$methName]["docstring"]),
  114.                         "string");
  115.     } else {
  116.       $r=new xmlrpcresp(new xmlrpcval("", "string"));
  117.     }
  118.   } else {
  119.       $r=new xmlrpcresp(0,
  120.               $xmlrpcerr["introspect_unknown"],
  121.               $xmlrpcstr["introspect_unknown"]);
  122.   }
  123.   return $r;
  124. }
  125.  
  126. $_xmlrpcs_dmap=array(
  127.                      "system.listMethods" =>
  128.                      array("function" => "_xmlrpcs_listMethods",
  129.                            "signature" => $_xmlrpcs_listMethods_sig,
  130.                            "docstring" => $_xmlrpcs_listMethods_doc),
  131.                      "system.methodHelp" =>
  132.                      array("function" => "_xmlrpcs_methodHelp",
  133.                            "signature" => $_xmlrpcs_methodHelp_sig,
  134.                            "docstring" => $_xmlrpcs_methodHelp_doc),
  135.                      "system.methodSignature" =>
  136.                      array("function" => "_xmlrpcs_methodSignature",
  137.                            "signature" => $_xmlrpcs_methodSignature_sig,
  138.                            "docstring" => $_xmlrpcs_methodSignature_doc)
  139.                      );
  140.  
  141. $_xmlrpc_debuginfo="";
  142. function xmlrpc_debugmsg($m) {
  143.   global $_xmlrpc_debuginfo;
  144.   $_xmlrpc_debuginfo=$_xmlrpc_debuginfo . $m . "\n";
  145. }
  146.  
  147. class xmlrpc_server {
  148.   var $dmap=array();
  149.  
  150.   function xmlrpc_server($dispMap, $serviceNow=1) {
  151.     // dispMap is a despatch array of methods
  152.     // mapped to function names and signatures
  153.     // if a method
  154.     // doesn't appear in the map then an unknown
  155.     // method error is generated
  156.     $this->dmap=$dispMap;
  157.     if ($serviceNow) {
  158.       $this->service();
  159.     }
  160.   }
  161.  
  162.   function serializeDebug() {
  163.     global $_xmlrpc_debuginfo;
  164.     if ($_xmlrpc_debuginfo!="")
  165.       return "<!-- DEBUG INFO:\n\n" .
  166.         $_xmlrpc_debuginfo . "\n-->\n";
  167.     else
  168.       return "";
  169.   }
  170.  
  171.   function service() {
  172.     $r=$this->parseRequest();
  173.     $payload="<?xml version=\"1.0\"?>\n" .
  174.       $this->serializeDebug() .
  175.       $r->serialize();
  176.     Header("Content-Type: text/xml\r\nContent-Length: " .
  177.            strlen($payload));
  178.     print $payload;
  179.   }
  180.  
  181.   function verifySignature($in, $sig) {
  182.     for($i=0; $i<sizeof($sig); $i++) {
  183.       // check each possible signature in turn
  184.       $cursig=$sig[$i];
  185.       if (sizeof($cursig)==$in->getNumParams()+1) {
  186.         $itsOK=1;
  187.         for($n=0; $n<$in->getNumParams(); $n++) {
  188.           $p=$in->getParam($n);
  189.           // print "<!-- $p -->\n";
  190.           if ($p->kindOf() == "scalar") {
  191.             $pt=$p->scalartyp();
  192.           } else {
  193.             $pt=$p->kindOf();
  194.           }
  195.           // $n+1 as first type of sig is return type
  196.           if ($pt != $cursig[$n+1]) {
  197.             $itsOK=0;
  198.             $pno=$n+1; $wanted=$cursig[$n+1]; $got=$pt;
  199.             break;
  200.           }
  201.         }
  202.       if ($itsOK)
  203.         return array(1);
  204.       }
  205.     }
  206.     return array(0, "Wanted ${wanted}, got ${got} at param ${pno})");
  207.   }
  208.  
  209.   function parseRequest($data="") {
  210.   global $_xh;
  211.   global $xmlrpcerr, $xmlrpcstr, $xmlrpcerrxml, $_xmlrpcs_dmap;
  212.  
  213.  
  214.  
  215.   if ($data=="") {
  216.     $data=$GLOBALS["HTTP_RAW_POST_DATA"];
  217.   }
  218.   $parser = drupal_xml_parser_create($data);
  219.  
  220.   $_xh[$parser]=array();
  221.   $_xh[$parser]['st']="";
  222.   $_xh[$parser]['cm']=0;
  223.   $_xh[$parser]['isf']=0;
  224.   $_xh[$parser]['params']=array();
  225.   $_xh[$parser]['method']="";
  226.  
  227.   // decompose incoming XML into request structure
  228.  
  229.   xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
  230.   xml_set_element_handler($parser, "xmlrpc_se", "xmlrpc_ee");
  231.   xml_set_character_data_handler($parser, "xmlrpc_cd");
  232.   xml_set_default_handler($parser, "xmlrpc_dh");
  233.   if (!xml_parse($parser, $data, 1)) {
  234.     // return XML error as a faultCode
  235.     $r=new xmlrpcresp(0,
  236.             $xmlrpcerrxml+xml_get_error_code($parser),
  237.             sprintf("XML error: %s at line %d",
  238.           xml_error_string(xml_get_error_code($parser)),
  239.           xml_get_current_line_number($parser)));
  240.     xml_parser_free($parser);
  241.   } else {
  242.     xml_parser_free($parser);
  243.     $m=new xmlrpcmsg($_xh[$parser]['method']);
  244.     // now add parameters in
  245.     $plist="";
  246.     for($i=0; $i<sizeof($_xh[$parser]['params']); $i++) {
  247.       //print "<!-- " . $_xh[$parser]['params'][$i]. "-->\n";
  248.       $plist.="$i - " .  $_xh[$parser]['params'][$i]. " \n";
  249.       eval('$m->addParam(' . $_xh[$parser]['params'][$i]. ");");
  250.     }
  251.     // uncomment this to really see what the server's getting!
  252.     // xmlrpc_debugmsg($plist);
  253.     // now to deal with the method
  254.     $methName=$_xh[$parser]['method'];
  255.     if (ereg("^system\.", $methName)) {
  256.       $dmap=$_xmlrpcs_dmap; $sysCall=1;
  257.     } else {
  258.       $dmap=$this->dmap; $sysCall=0;
  259.     }
  260.     if (isset($dmap[$methName]['function'])) {
  261.       // dispatch if exists
  262.       if (isset($dmap[$methName]['signature'])) {
  263.         $sr=$this->verifySignature($m,
  264.                                   $dmap[$methName]['signature'] );
  265.       }
  266.       if ( (!isset($dmap[$methName]['signature']))
  267.            || $sr[0]) {
  268.         // if no signature or correct signature
  269.         if ($sysCall) {
  270.           eval('$r=' . $dmap[$methName]['function'] .
  271.                '($this, $m);');
  272.         } else {
  273.           eval('$r=' . $dmap[$methName]['function'] .
  274.                '($m);');
  275.         }
  276.       } else {
  277.         $r=new xmlrpcresp(0,
  278.               $xmlrpcerr["incorrect_params"],
  279.               $xmlrpcstr["incorrect_params"].": ". $sr[1]);
  280.       }
  281.     } else {
  282.     // else prepare error response
  283.     $r=new xmlrpcresp(0,
  284.               $xmlrpcerr["unknown_method"],
  285.               $xmlrpcstr["unknown_method"]);
  286.     }
  287.   }
  288.   return $r;
  289.   }
  290.  
  291.   function echoInput() {
  292.  
  293.   // a debugging routine: just echos back the input
  294.   // packet as a string value
  295.  
  296.   $r=new xmlrpcresp;
  297.   $r->xv=new xmlrpcval( "'Aha said I: '" . $GLOBALS["HTTP_RAW_POST_DATA"], "string");
  298.   print $r->serialize();
  299.   }
  300. }
  301.  
  302. ?>
  303.